package com.communal.service.impl.tarif;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.communal.dao.ICommunalDao;
import com.communal.dao.IModelDao;
import com.communal.model.Model;
import com.communal.model.cr.CRWater;
import com.communal.model.tarif.TarifWaterHot;
import com.communal.service.ITarifService;
import com.communal.util.AppConstants;
import com.communal.util.AppException;
import com.communal.web.form.tarif.TarifWaterForm;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Service
public class TarifWaterHotService extends TarifService implements ITarifService<TarifWaterForm>{

	private final String LOG_ENTITTY = "water hot tarif";
	
	@Autowired
	ICommunalDao communalDao;
	
	@Autowired
	IModelDao modelDao;
	
	@Override
	@Transactional(readOnly=true)
	public List<TarifWaterForm> getRecords(Model p_model) throws AppException {
		checkModel( p_model );
		List<TarifWaterForm> tarifFormList = new ArrayList<TarifWaterForm>();
		try{
			List<TarifWaterHot> tarifs = modelDao.getEnabledEntitiesByModel( p_model, TarifWaterHot.class );
			if(CollectionUtils.isNotEmpty(tarifs)){
				for(TarifWaterHot tarif : tarifs){
					tarifFormList.add(new TarifWaterForm( tarif ));
				}		
			}
		}catch (Exception err) {
			log.error(messageSource.getMessage("log.error.load", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));
		}
		return tarifFormList;
	}

	@Override
	@Transactional(readOnly=false)
	public List<TarifWaterForm> saveOrUpdateRecord(Object p_data, Model p_model) throws AppException {
		checkModel( p_model );
		List<TarifWaterForm> tarifForms = null;
		try{
			TarifWaterForm form = new TarifWaterForm( p_data );
			form.setModelId(p_model.getId());
			
			if(form.getId().equals(AppConstants.FAKE_ID)){
				form.setId( null );
			}
			validateTarifName( p_model, TarifWaterHot.class, form.getTarifName() );
			tarifForms = new ArrayList<TarifWaterForm>();
			tarifForms.add(new TarifWaterForm( 
					(TarifWaterHot) baseDao.saveOrUpdate(createTarif( form, p_model )) ));
		}catch (AppException err) {
			throw new AppException( err.getMessage() );
		}catch (Exception err) {
			log.error(messageSource.getMessage("log.error.save", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));
		}
		return tarifForms;
	}

	@Override
	public void deleteRecords(Object p_data, Model p_model) throws AppException {
		checkModel( p_model );
		try{
			List<TarifWaterHot> tarifs = modelDao.getEnabledEntitiesByModel( p_model, TarifWaterHot.class );
			if(CollectionUtils.isNotEmpty( tarifs ) && tarifs.size() == 1 ){
				throw new AppException(messageSource.getMessage("message.error.tarif.one", null, null));
			}
			
			Integer id = Integer.parseInt(p_data.toString());
			TarifWaterHot tarif = (TarifWaterHot) baseDao.getById(TarifWaterHot.class, id);
			if(tarif != null){
				List<CRWater> records = communalDao.getWaterRecordsByTarif( p_model, null, tarif );
				if(CollectionUtils.isNotEmpty( records )){
					throw new AppException(messageSource.getMessage("message.error.tarif.records", null, null));
				}
				tarif.setEnabled( AppConstants.ENTITY_STATE_DISABLED );
				baseDao.saveOrUpdate( tarif );
			}	
		}catch (AppException err) {
			throw new AppException( err.getMessage() );
		}catch (Exception err) {
			log.error(messageSource.getMessage("log.error.delete", new Object[]{err.getMessage(), LOG_ENTITTY}, null));
			throw new AppException(messageSource.getMessage("message.error.common", null, null));	
		}
	}

	/**
	 * Creates water tarif from form parameters for current model <br>
	 * @param p_form - water tarif form; {@link TarifWaterForm} <br>
	 * @param p_model - model; {@link IModel} <br>
	 * @return water tarif; {@link TarifWaterHot}
	 */
	private TarifWaterHot createTarif(TarifWaterForm p_form, Model p_model){
		TarifWaterHot tarif = new TarifWaterHot();
		tarif.setId( p_form.getId() );
		tarif.setEnabled( AppConstants.ENTITY_STATE_ENABLED );
		tarif.setModel( p_model );
		tarif.setTarifName( p_form.getTarifName() );
		tarif.setType( p_form.getTarifType() );
		tarif.setSumma( p_form.getSumma() );
		return tarif;
	}
}
